<?php

namespace app\api\service;

use EasyWeChat\Factory;
use app\api\model\User as UserModel;
use think\Db;

class Message
{
    private $app;

    public function __construct()
    {
        $config = [
            'app_id' => 'wx8468f37d615992c0',
            'secret' => '70dab87efc8320480ab94c9826a56557',
            'token' => 'wx471e1e5dfb78fbb7',
            'aes_key' => 'dQHN7KcmVAlJflcWpmLN3It6UVIukcHjUpnLbB697p9',
            'response_type' => 'array',
        ];

        $this->app = Factory::officialAccount($config);
    }

    /**
     *
     */
    public function index()
    {
        // $info=$this->get();
        // echo '<pre>';
        // var_dump($info);
        // exit;
        // o1vlg6dXGWDubKrqyqmujx7ddRtg
        $list = $this->app->user->list($nextOpenId = null);  // $nextOpenId 可选
//         echo "<pre>";
//         var_dump($list);die;
        foreach ($list['data']['openid'] as $k => $v) {
            $this->get($v);
        }
    }

    public function get($open_id = "o1vlg6UFcwZTb1j3Gm2Fgw98oh3U")
    {
        $wx_info = $this->app->user->get($open_id);
        $detail = UserModel::useGlobalScope(false)->where(['unionid' => $wx_info['unionid']])->find();
        if ($detail) {
            echo $wx_info['openid'] . '<br>';
            $detail->save(['gz_open_id' => $wx_info['openid']]);
        }
        return true;
    }

    /**
     * @param $params
     * @return bool
     */
    public function send($params)
    {
        $this->app->template_message->send([
            'touser' => $params['gz_open_id'],//o1vlg6aqs93gM6pFRoTlb14Sd4pg
            'template_id' => 'EUAV3WMJQ3DB7QEgHyrkxVfhlg4f0jmhn79S1Lgpf0M',
            'url' => '',
            'miniprogram' => [
                'appid' => 'wx471e1e5dfb78fbb7',
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'first' => ['value' => '约课提醒', 'color' => '#550038'],
                'keyword1' => ['value' => $params['nickName'], 'color' => '#550038'],
                'keyword2' => ['value' => $params['coachName'], 'color' => '#550038'],
                'keyword3' => ['value' => $params['reservation_time'], 'color' => '#550038'],
                'remark' => ['value' => $params['remark']],
            ],
        ]);

        return true;
    }


    public function notice($params)
    {
        // sun o1vlg6XIEGtVHs8QWovZnd-50RaE
        //o1vlg6aqs93gM6pFRoTlb14Sd4pg
        $today = date('Y-m-d');
        $this->app->template_message->send([
            'touser' => 'o1vlg6XIEGtVHs8QWovZnd-50RaE',//o1vlg6aqs93gM6pFRoTlb14Sd4pg
            'template_id' => 'ajRkX2Z1FKb0nClIz4WgK2Xuj0mIRMgrxhTTdcuaLnc',
            'url' => '',
            'miniprogram' => [
                'appid' => 'wx471e1e5dfb78fbb7',
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'first' => ['value' => '消息通知', 'color' => '#550038'],
                'keyword1' => ['value' => 'mod拳击', 'color' => '#550038'],
                'keyword2' => ['value' => $params['remark'], 'color' => '#550038'],
                'keyword3' => ['value' => $today, 'color' => '#550038'],
                'keyword4' => ['value' => '', 'color' => '#550038'],
                'keyword5' => ['value' => '', 'color' => '#550038'],
                'remark' => ['value' => $params['remark']],
            ],
        ]);
        return true;
    }

    //取消预约通知教练
    public function noticeCancel($params)
    {
        // sun o1vlg6XIEGtVHs8QWovZnd-50RaE
        //o1vlg6aqs93gM6pFRoTlb14Sd4pg
        $today = date('Y-m-d');
        $this->app->template_message->send([
            'touser' => $params['gz_open_id'],//o1vlg6aqs93gM6pFRoTlb14Sd4pg
            'template_id' => 'OVO23zST_ekDI6rMqNMEWiFfRW9zOhsJmVMtn7xABB4',
            'url' => '',
            'miniprogram' => [
//                'appid' => 'wx471e1e5dfb78fbb7',
//                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'first' => ['value' => '预约取消通知', 'color' => '#550038'],
                'keyword1' => ['value' => $params['nickName'], 'color' => '#550038'],
                'keyword2' => ['value' => $params['phone'], 'color' => '#550038'],
                'keyword3' => ['value' => $params['course_name'], 'color' => '#550038'],
                'keyword4' => ['value' => $params['start_time'], 'color' => '#550038'],
                'keyword5' => ['value' => $params['cancel_time'], 'color' => '#550038'],
                'remark' => ['value' => $params['remark']],
            ],
        ]);
        return true;
    }

    /**
     * @title 开课提醒
     * @param $params
     * @author Qian
     * Time: 2023/7/6  16:57
     */
    public function lesson($params)
    {
        $this->app->template_message->send([
            'touser' => $params['gz_open_id'],//o1vlg6aqs93gM6pFRoTlb14Sd4pg
            'template_id' => 'C04eBJJ49y1dnIupjJbwqz2VxDpByEoT1kvkFx3QlGc',
            'url' => '',
            'miniprogram' => [
                'appid' => '',
                'pagepath' => '',
            ],
            'data' => [
                'thing4' => ['value' => $params['course_name'], 'color' => '#550038'],
                'time5' => ['value' => $params['start_time'], 'color' => '#550038'],
            ],
        ]);

        return true;
    }

    public function data()
    {
        $today = date('Y-m-d');
        $model = new \app\store\model\Store();
        $sale = bcadd($model->getOrderTotalPrice($today), $model->getCourseOrderTotalPrice($today), 2);
        $string = "销售额:$sale";

        $sql = "select count('id') as num
from yoshop_course_reservation as r
where r.status = 2 ";
        $sql .= " and r.`date` >= '" . $today . "' ";
        $sql .= " and r.`date` <= '" . $today . " 23:59:59" . "' ";
        $list = Db::query($sql);
        $string .= ",课时消耗:" . $list['0']['num'];

        $sql = "select r.coach_id,r.`date`,r.shop_id,ycod.goods_name,ycod.nums,ycod.total_price,ycod.type,
       yss.shop_name,yc.real_name,r.course_order_id
from yoshop_course_reservation as r
left join yoshop_course_order yco on r.course_order_id = yco.order_id
left join yoshop_course_order_detail ycod on r.course_order_id = ycod.course_order_id
left join yoshop_store_shop yss on yss.shop_id=r.shop_id
left join yoshop_coach yc on r.coach_id = yc.coach_id
where r.status = 2 and yco.is_delete=0 ";
        $sql .= " and r.`date` >= '" . $today . "' ";
        $sql .= " and r.`date` <= '" . $today . " 23:59:59" . "' ";
        $list = Db::query($sql);
        $total = "0.00";
        if (!empty($list)) {
            foreach ($list as $value) {
                $total = bcadd($total, bcdiv($value['total_price'], $value['nums'], 2), 2);
            }
        }
        $string .= ",课时价值:$total";
        return $this->notice(['remark' => $string]);

    }
}